$(function(){
	initBlockUI();
	getCompanyListViaJs("#com_select", -1);
	getSectionListViaJs("#sec_select", -1, -1);
	$("#com_select").change(function(){
		getSectionListViaJs("#sec_select", $(this).val(), -1);
	});
   $("#sec_select").change(function(){
	  var com_name = $("#com_select")
		.find("option[@value='" + $("#com_select").val() + "']")
		  .html();
	  var sec_name = $(this)
		.find("option[@value='" + $(this).val() + "']")
		  .html();
	  $("#sec_name").html(com_name + " -&gt; " + sec_name);
	  showUsers($(this).val());
   });
});

// 更新数据表格
function showUsers(section_id)
{
   var tbody = $("#tbody2");
   var url = "user_power.php";
   var pars = { "page_status": 1, "section_id": section_id };
   
   $.post(url, pars, function(xmlobj){
	  tbody.html("");
	  $(xmlobj).find("item").each(function(i){
		 var item_id = $(this).find("id").text();
		 var item_name = $(this).find("name").text();
		 var item_power = $(this).find("power").text();
		 tbody.append($("<tr></tr>")
		 			  .append($("<th></th>").html(i + 1))
					  .append($("<td></td>").html(item_name))
					  .append($("<td></td>")
							  .html($("<a href=\"#\"></a>")
							  		.attr("uid", item_id)
							  		.attr("power", item_power)
							  		.html("查看")
							  		.click(function(){
							  			showPower($(this).attr("uid"), $(this).attr("power"));
							  			return false;
							  		})))
					  .append($("<td></td>")
							  .html($("<a href=\"#\"></a>")
							  		.attr("uid", item_id)
							  		.attr("power", item_power)
							  		.html("修改")
							  		.click(function() {
							  			modifyPower($(this).attr("uid"), $(this).attr("power"), this);
							  			return false;
							  		})
							  )));
	  });
	  tablePretty("#tbody2");
   });
}

// 显示权限
function showPower(sec_id, power)
{
   var pars_power = power;
   var power_array = (pars_power != "") ? (arrayUnique(pars_power.split(",")).sort()) : new Array();
   var power_list = $("<div></div>").addClass("power_list")
	 .append($("<div></div>").addClass("modal_header").html("现有权限："))
	 .append($("<div></div>").addClass("modal_content").html(getPowerListViaJs(power_array)));
   $.modal(power_list, {
		onShow: function() { m(1); },
		onClose: function() { m(0); }
	});
}

// 修改权限
function modifyPower(uid, power, block)
{
	var pl = [];
	$.each(__powerArray__, function(i, n) {
		pl[pl.length] = n[3];
	});
	var power_block = $("<div></div>").addClass("modify_modal")
		.append($("<div></div>").addClass("modal_header").html("修改权限"))
		.append($("<div></div>").addClass("modal_content").html(makePowerCheckBox(pl, $(block).attr("power").split(","), "power_check", "pc[]")))
		.append($("<div></div>").addClass("modal_footer").append(
			$("<input type=\"button\" />").val("提交")
				.click(function(){
					var powerlist = [];
					$("#power_check input:checkbox[@name='pc\[\]']:checked").each(function(i){
						powerlist[powerlist.length] = $(this).val();
					});
					var url = "user_power.php";
					var pars = { page_status: 2, uid: uid, power: powerlist.join(",") };
					$.post(url, pars, function(text) {
						if (/^Success/.test(text))
						{
							$(block).parent("td").prev("td").find("a").attr("power", pars["power"]);
							$(block).attr("power", pars["power"]);
							m(0);
							$.modal.close();
						}
					});
				})
		));
	$.modal(power_block, {
		onShow: function() { m(1); },
		onClose: function() { m(0); }
	});
}

// 控制 modal 隐现时表单的可视度
function m(flag) { modalShowClose(".left select", flag); }
